package team.kirohuji.repository.jpa.user;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import team.kirohuji.domain.user.User;

/**
 * 这个类用来对权限和菜单映射的管理
 * 
 * @author kirohuji
 * 
 */
@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
	
	// 查询 后台用户的所有权限 priority_type=0 代表 后台权限，=1代表微信客户端权限
	@Query(value="select p.perms from user_role ur"
			+ " join priority_role pr on ur.frole_id=pr.frole_id "
			+ " join priority p on pr.fpriority_id=p.priority_id "
			+ " where ur.fuser_id=?1 and p.priority_type=0",nativeQuery=true)
	List<String> queryAllPerms(Integer userId);
	
	
	
	@Query(value="select * from user where login_name=?1",nativeQuery=true)
	User queryByLoginName(String username);
	
	@Query(value="select p.priority_id from user_role ur join priority_role pr on ur.frole_id = pr.frole_id join priority p on pr.fpriority_id = p.priority_id where ur.fuser_id = ?1 and p.priority_type = 0",nativeQuery=true)
	List<Integer> queryAllMenuId(Integer userId);

}